#!/bin/sh

for dir in /run/_tmp /run/_var/tmp /run/_var/cache /run/_var/log /run/_var/run;
do
    mkdir -p $dir
    chmod 1777 $dir
done

if ! [ -d /var/run/sshd ]; then
    mkdir -p /var/run/sshd
fi

if ! [ -d /var/log/dpkg ]; then
    mkdir -p /var/log/dpkg
fi

if ! [ -d /work/portainer ]; then
    mkdir -p /work/portainer
fi

if [ "$JUPYTER_ENABLE" = "1" ]; then
    install -o $WORKUSER -m 700 -d /run/jupyter-runtime-$WORKUSER
    sudo -u $WORKUSER PWD=/work JUPYTER_RUNTIME_DIR=/run/jupyter-runtime-$WORKUSER /home/$WORKUSER/.local/bin/jupyter lab --ip=* --port=8002 --no-browser --notebook-dir=/work --app-dir=~/.local/share/jupyter/lab &
fi

if [ "$CLOUD9_ENABLE" = "1" ]; then
    sudo -u $WORKUSER /home/$WORKUSER/cloud9/server.js --listen 0.0.0.0 --port 8003 -w /work &
fi

portainer_dat_dir=/work/portainer
portainer_cert=$portainer_dat_dir/certs/portainer.crt
portainer_key=$portainer_dat_dir/certs/portainer.key
portainer_cmd="/portainer/portainer -a /portainer -d $portainer_dat_dir -p :8001"

if [ -f $portainer_cert -a -f $portainer_key ]; then
    portainer_cmd="$portainer_cmd --ssl --sslcert $portainer_cert --sslkey $portainer_key"
fi

$portainer_cmd &
exec /usr/sbin/sshd -p 8000 -D

